<?php

namespace plugin\admin\app\controller;

use app\model\GoodsCateModel;
use support\exception\BusinessException;
use support\Request;
use support\Response;
use Throwable;

/**
 * 商品分类
 */
class RlGoodsCateController extends Crud
{

    /**
     * @var GoodsCateModel
     */
    protected $model = null;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new GoodsCateModel;
    }

    /**
     * 浏览
     * @return Response
     * @throws Throwable
     */
    public function index(): Response
    {
        return raw_view('rl-goods-cate/index');
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        if ($request->get('format','')=='tree'){
            $trees=$this->model->selectTree();
            return $this->json(0,'成功',$trees);
        }
        return parent::select($request);
    }

    /**
     * 查询前置方法
     * @param Request $request
     * @return array
     * @throws BusinessException
     */
    protected function selectInput(Request $request): array
    {
        [$where, $format, $limit, $field, $order] = parent::selectInput($request);
        // 默认weight排序
        if (!$field) {
            $field = 'cate_sort';
            $order = 'desc';
        }
        return [$where, $format, $limit, $field, $order];
    }

    /**
     * 添加
     * @param Request $request
     * @return Response
     * @throws BusinessException|Throwable
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'GET') {
            return raw_view('rl-goods-cate/insert');
        }
        //获取数据
        $cateTitle=$request->post('cate_title','');
        $cateIcon=$request->post('cate_icon','');
        $cateId=$request->post('cate_id',0)?:0;
        $status=$request->post('status',2)?:2;
        $ret=GoodsCateModel::insert([
            'cate_title'=>$cateTitle,
            'cate_icon'=>$cateIcon,
            'cate_id'=>$cateId,
            'status'=>$status,
            'create_time'=>time()
        ]);
        if (!$ret){
            return $this->json(-1);
        }
        return $this->json(0);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException|Throwable
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'GET') {
            return raw_view('rl-goods-cate/update');
        }
        $id=$request->post('id',0);
        //获取数据
        $cateTitle=$request->post('cate_title','');
        $cateIcon=$request->post('cate_icon','');
        $cateId=$request->post('cate_id',0)?:0;
        $status=$request->post('status',1)?:1;
        $update=[
            'cate_title'=>$cateTitle,
            'cate_icon'=>$cateIcon,
            'cate_id'=>$cateId,
            'status'=>$status,
            'update_time'=>time()
        ];
        $update=array_filter($update);
        $ret=GoodsCateModel::where('id',$id)->update($update);
        if (!$ret){
            return $this->json(-1);
        }
        return $this->json(0);
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doDelete($ids);
        return $this->success('删除成功');
    }

}
